package com.school.utils;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import java.util.Map;

/**
 * JDBC工具类
 */
public class JdbcUtils {
    
    /**
     * 创建Bean属性参数源
     */
    public static SqlParameterSource createBeanParams(Object bean) {
        return new BeanPropertySqlParameterSource(bean);
    }
    
    /**
     * 创建Map参数源
     */
    public static SqlParameterSource createMapParams(Map<String, ?> params) {
        return new MapSqlParameterSource(params);
    }
    
    /**
     * 创建MapSqlParameterSource
     */
    public static MapSqlParameterSource createParams() {
        return new MapSqlParameterSource();
    }
    
    /**
     * 创建MapSqlParameterSource并添加参数
     */
    public static MapSqlParameterSource createParams(String key, Object value) {
        return new MapSqlParameterSource(key, value);
    }
    
    /**
     * 创建BeanPropertyRowMapper
     */
    public static <T> BeanPropertyRowMapper<T> createRowMapper(Class<T> clazz) {
        return new BeanPropertyRowMapper<>(clazz);
    }
}


